//
//  NewsListViewController.m
//  MedicalTip
//
//  Created by 徐潇 on 15/4/13.
//  Copyright (c) 2015年 徐潇. All rights reserved.
//

#import "NewsListViewController.h"

@interface NewsListViewController ()

@end

@implementation NewsListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
//    NSLog(@"woshi list");
//    NewsList *new = [self.newsArray objectAtIndex:0];
//    NSLog(@"%@", new.tag);
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.myTableView.backgroundColor = [UIColor whiteColor];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    [_myTableView release];

    [self addHeader];
    [self addFooter];
    
}
#pragma mark --下拉刷新
- (void)addHeader
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block NewsListViewController *vc = self;
    
    // 添加下拉刷新头部控件
    [self.myTableView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        
        NSLog(@"下拉刷新，开始网络请求第一页数据");
        [MyAFNBlock getDataWithString:@"http://api.yi18.net/news/list?page=1&limit=20&id=3" block:^(id backValue) {
           // NSLog(@"====%@", backValue);
            NSMutableDictionary *Dic = backValue;
            self.newsArray = [NSMutableArray array];
            NSMutableArray *array = [Dic objectForKey:@"yi18"];
            for (NSDictionary *dic in array) {
                NewsList *newsList = [[NewsList alloc] init];
                [newsList setValuesForKeysWithDictionary:dic];
                newsList.listId = [dic objectForKey:@"id"];
                [self.newsArray addObject:newsList];
            }
            [self.myTableView reloadData];
        }];

        
        //（为了模拟延迟加载数据，因此2秒后才让刷新请求结束，实际项目中可以在数据请求成功后，停止下拉效果）
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [vc.myTableView reloadData];
            // 结束刷新
            [vc.myTableView headerEndRefreshing];
        });
        
        
    }];
    
#pragma mark  --自动刷新(一进入程序就下拉刷新)
    [self.myTableView headerBeginRefreshing];
}

#pragma mark --上拉加载更多
- (void)addFooter
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block NewsListViewController *vc = self;
    
    // 添加上拉刷新尾部控件
    [self.myTableView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        NSLog(@"上拉加载更多，开始网络请求更多数据");
        
        // （模拟延迟加载数据，因此2秒后才调用）
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [vc.myTableView reloadData];
            // 结束刷新
            [vc.myTableView footerEndRefreshing];
        });
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.newsArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    NewsShowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[NewsShowTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
    }
    NewsList *list = [self.newsArray objectAtIndex:indexPath.row];
    cell.newsLable.text = list.title;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
